<?php get_header(); ?>

<div id="main_body">
    <div class="container">
        <div class="col_left">
        
        <div id="middle">
        <div class="articles">
            <?php if (have_posts()) : ?>
   
               <h2> <?php single_cat_title() ?></h2>         
                <?php 
                $pid = get_query_var('cat');
                $c = get_category($pid); //get category by id
                $slug = $c->slug; //choose slug of category for create link
                
                $cats = get_categories(array('type' => 'post', 'child_of' => $pid, 'hide_empty' => 1, 'orderby' => 'term_id', 'order' => 'DESC'));
                 
                if (!empty($cats)) {

                    $cats = hc_sort_category($pid, $cats);

                    $post_id = main_article(); //display  main article
                    
                    foreach ($cats as $cat) {
                        ?>
               <div class="cat_list">
                        <h3><a href="<?php echo WP_HOME . '/category/' . $slug . '/' . $cat->slug ?>"><?php echo $cat->name ?></a><span> &raquo;</span></h3>
                        <?php
                        query_posts("cat=$cat->cat_ID&posts_per_page=3");
                        display_posts($post_id); //display by category
                        // Reset Query
                        wp_reset_query();
                        ?>
               </div>
                        <?php
                    }
                } else {
                    $i = 0;
                    while (have_posts()) : the_post();
                        $i += 1;
                        if ($i == 1) {
                            main_article(); //display main article
                        } else {
                        ?>

                        <div <?php post_class() ?>>
                            <div class="post-thumbnail">
                                <a class="readmore" href="<?php the_permalink(); ?>"><?php the_post_thumbnail('thumbnail'); ?></a>
                            </div>
                            <div class="post-content">
                                <h3 id="post-<?php the_ID(); ?>"><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h3>

                                        <?php //include (TEMPLATEPATH . '/inc/meta.php' );   ?>

                                <div class="entry">
                                    <p>
                                    <?php echo get_the_excerpt(); ?>
                                        ...<a class="readmore" href="<?php the_permalink(); ?>">អាន​លម្អិត</a>
                                    </p>
                                </div>
                            </div>
                            <div class="clearfix"></div>

                        </div>

                    <?php 
                            }  
                        endwhile; 
                        
                    ?>
                    <?php if (function_exists('wp_pagenavi')) wp_pagenavi(); ?>
                    <?php 
                }   ?>


            <?php else : ?>

                <h2>ទំព័រ​កំពុង​រៀបចំ សូម​មេត្តា​ចូល​អាន​ពេល​ឆាប់ៗនេះ</h2>
				<div class="aligncenter">
					<img src="http://www.healthcambodia.com/This-Section-is-Under-Construction.gif" alt="" />
				</div>

    <?php endif; ?>
        </div><!-- articles -->
        
    </div> <!-- middle -->
    <?php get_sidebar(); ?>
        </div> <!-- .col-left -->
    <div class="col_right">
        <?php get_sidebar('right'); ?>
    </div><!-- .col_right -->
</div>
<div class="clearfix"></div>
</div><!-- #main -->
<?php get_footer(); ?>